package com.yang.utils;

import com.yang.pojo.Content;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@Component
public class HtmlParseUtils {
    public static void main(String[] args) throws IOException {
        List<Content> contentList = new HtmlParseUtils().searchBykey("java");
        contentList.forEach(System.out::println);
    }

    /**
     * 爬虫查询京东相关数据
     *
     * @param keywords
     * @return
     * @throws IOException
     */
    public List<Content> getByKeywords(String keywords) throws IOException {
        List<Content> resultList = new ArrayList<>();
        // 获取请求 https://search.jd.com/Search?keyword=java
        // 前提，需要联网
        String url = "https://search.jd.com/Search?keyword=" + keywords + "&enc=utf-8";
        // 解析网页，（jsoup返回的document就是浏览器document对象）
        Document document = Jsoup.parse(new URL(url), 30000);
        // 所有在js中可以使用的都能使用
        Element element = document.getElementById("J_goodsList");
        // System.out.println(element.html());
        // 获取所有的li元素
        Elements elements = element.getElementsByTag("li");
        // 获取元素中的内容, 这里el就是每一个li标签了
        for (Element currentElement : elements) {
            // 获取img标签里面的第一个元素, 其属性为src属性
            String img = currentElement.getElementsByTag("img").eq(0).attr("data-lazy-img");
            // String img = currentElement.getElementsByTag("img").eq(0).attr("src");
            String price = currentElement.getElementsByClass("p-price").eq(0).text();
            String title = currentElement.getElementsByClass("p-name").eq(0).text();
            // String img_lazy = currentElement.getElementsByClass("data-lazy-img").eq(0).attr("src");
            // System.out.println(" ======================= ");
            // System.out.println("title = " + title);
            // System.out.println("price = " + price);
            // System.out.println("img = " + img);
            // System.out.println(" ======================= ");
            Content content = Content.builder().img(img).price(price).title(title).build();
            resultList.add(content);
        }
        return resultList;
    }

    /**
     * 搜索相关数据
     *
     * @param keyword
     * @return
     * @throws IOException
     */
    public List<Content> searchBykey(String keyword) throws IOException {
        String url = "https://search.jd.com/Search?keyword=" + keyword + "&enc=utf-8";
        Document document = Jsoup.parse(new URL(url), 30000);
        Element element = document.getElementById("J_goodsList");
        Elements elements = element.getElementsByTag("li");
        List<Content> contentList = new ArrayList<>();
        for (Element currentElement : elements) {
            String img = currentElement.getElementsByTag("img").eq(0).attr("data-lazy-img");
            String price = currentElement.getElementsByClass("p-price").eq(0).text();
            String title = currentElement.getElementsByClass("p-name").eq(0).text();
            Content content = Content.builder().img(img).title(title).price(price).build();
            contentList.add(content);
        }
        return contentList;
    }

}